package com.ruoyi.web.controller.api;

import com.ruoyi.common.utils.MD5Util;
import com.ruoyi.system.domain.SysOrder;
import com.ruoyi.system.domain.SysStore;
import com.ruoyi.system.service.ISysOrderService;
import com.ruoyi.system.service.ISysStoreService;
import com.ruoyi.web.controller.utils.JR;
import com.ruoyi.web.controller.utils.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysOrderService {
    public static Logger logger = LoggerFactory.getLogger(RechargeApiController.class);
    @Autowired
    private ISysStoreService sysStoreService;

    @Autowired
    private ISysOrderService sysOrderService;

    public JR storeRecharge(StoreRechargeDTO dto){
        List<StoreRechargeChildDTO> childList = dto.getChildList();
        //充值总金额
        int totalRechargeMoney = 0;
        for (StoreRechargeChildDTO child:childList){
            if(child.getAmount() == null){
                logger.info("充值用户"+child.getMobile()+"充值金额不能为空");
                return  JR.build(JsonResult.STATUS_PARAM_ERROR,"充值用户"+child.getMobile()+"充值金额不能为空",JsonResult.STATUS_PARAM_ERROR);
            }
            totalRechargeMoney = totalRechargeMoney + child.getAmount();
        }
        SysStore sysStore = sysStoreService.checkStore(dto.getStoreCode());
        if(sysStore == null){
            logger.info("商户信息不存在");
            return  JR.build(JsonResult.STATUS_PARAM_ERROR,"商户信息不存在",JsonResult.STATUS_PARAM_ERROR);
        }
        //签名校验   签名 md5(充值总金额 + code + createTime + secretKey) 保住顺序
        String md5Key =totalRechargeMoney + dto.getStoreCode() + dto.getCreateTime() + sysStore.getSecretKey();
        String sign = MD5Util.toMD5(md5Key);
        if(!sign.equals(dto.getSign())){
            logger.info("签名信息有误");
            return  JR.build(JsonResult.STATUS_PARAM_ERROR,"签名信息有误",JsonResult.STATUS_PARAM_ERROR);
        }
        List<SysOrder> orderList = new ArrayList<>();
        SysOrder sysOrder = null;
        for (StoreRechargeChildDTO tempDto: dto.getChildList()) {
            sysOrder = new SysOrder();
            sysOrder.setUserMobile(tempDto.getMobile());
            sysOrder.setUserAmount(new BigDecimal(tempDto.getAmount()));
            sysOrder.setStoreOrderNo(tempDto.getOrderNo());
            sysOrder.setStoreCode(sysStore.getCode());
            sysOrder.setStoreName(sysStore.getName());
            // lzm undo sysOrder.setCreateBy(dto.getCreateBy());
            sysOrder.setStoreOrderTime(new Date());
            sysOrder.setCreateTime(new Date());
            sysOrder.setStoreOrderTime(new Date());
            orderList.add(sysOrder);
        }
        try{
            sysOrderService.bathRecharge(orderList);
            return JR.ok();
        }catch (Exception e){
            logger.error("充值异常："+e.getMessage());
            return JR.build(500,"充值失败",500);
        }
    }
}
